/*
 * 文件名： SerializeUtils.java
 * 
 * 创建日期： 2016年5月30日
 *
 * Copyright(C) 2016, by <a href="mailto:liws@xingyuanauto.com">liws</a>.
 *
 * 原始作者: liws
 *
 */
package com.moli.iov.util;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * 对象序列化工具类 对象序列化工具类
 *
 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
 *
 * @version $Revision$
 *
 * @since 2016年5月30日
 */
public class SerializeUtils {

	final static Logger log = LoggerFactory.getLogger(SerializeUtils.class);

	public static byte[] serialize(Object obj) {
		return fstSerialize(obj);
		 //return kryoSerizlize(obj);
		//return jdkSerialize(obj);
	}

	public static Object deserialize(byte[] sec) {
		 return fstDeSerialize(sec);
		 //return kryoDeSerizlize(sec);
		//return jdkDeSerialize(sec);
	}

	public static byte[] fstSerialize(Object obj) {
		ByteArrayOutputStream out = null;
		FSTObjectOutput fout = null;
		try {
			out = new ByteArrayOutputStream();
			fout = new FSTObjectOutput(out);
			fout.writeObject(obj);
			fout.flush();
			return out.toByteArray();
		}
		catch (IOException e) {
			log.error(e.getMessage());
			return null;
		}
		finally {
			if (fout != null)
				try {
					fout.close();
				}
				catch (IOException e) {
				}
		}
	}

	public static Object fstDeSerialize(byte[] bytes) {
		if (bytes == null || bytes.length == 0)
			return null;
		FSTObjectInput in = null;
		try {
			in = new FSTObjectInput(new ByteArrayInputStream(bytes));
			return in.readObject();
		}
		catch (ClassNotFoundException e) {
			log.error(e.getMessage());
			return null;
		}
		catch (IOException e) {
			log.error(e.getMessage());
			return null;
		}
		finally {
			if (in != null)
				try {
					in.close();
				}
				catch (IOException e) {
				}
		}
	}

	public static byte[] kryoSerizlize(Object obj) {
		Kryo kryo = new Kryo();
		Output output = null;
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			output = new Output(baos);
			kryo.writeClassAndObject(output, obj);
			output.flush();
			return baos.toByteArray();
		}finally{
			if(output != null)
				output.close();
		}
	}

	public static Object kryoDeSerizlize(byte[] bytes) {
		if(bytes == null || bytes.length == 0)
			return null;
		Kryo kryo = new Kryo();
		Input ois = null;
		try {
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			ois = new Input(bais);
			return kryo.readClassAndObject(ois);
		} finally {
			if(ois != null)
				ois.close();
		}
	}

	// jdk原生序列换方案
	public static byte[] jdkSerialize(Object obj) {
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ObjectOutputStream oos = new ObjectOutputStream(baos);) {
			oos.writeObject(obj);
			return baos.toByteArray();
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static Object jdkDeSerialize(byte[] bits) {
		try (ByteArrayInputStream bais = new ByteArrayInputStream(bits);
				ObjectInputStream ois = new ObjectInputStream(bais);

		) {
			return ois.readObject();
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	
	public static void main(String[] args) {
	}
}